/*
 * Copyright (C) 2015-2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


/**
 * @file
 * @brief Functions to manage strings in C.
 */


#ifndef PLANET_WARS_2D_RT_CORE_CSTRING_FUNCTIONS_H
#define PLANET_WARS_2D_RT_CORE_CSTRING_FUNCTIONS_H


#include <string.h>
#include "PlanetWars2dRT-core/utils/c_language.h"
#include "PlanetWars2dRT-core/utils/bool.h"
#include "PlanetWars2dRT-core/utils/compiler_specific.h"


EXTERN_C_BEGIN


/**
 * @brief Check if 2 strings are equal.
 * @param str1 A string
 * @param str2 An other string
 * @return True if the first string equals the other one, otherwise false
 */
bool
cstring_equals(const char* str1,
	       const char* str2);

bool
cstring_starts_with(const char* a_string,
		    const char* potential_prefix);

bool
cstring_ends_with(const char* base,
		  const char* str);

/**
 * @brief Reverse the order of characters in the string.
 * @param string String to reverse
 */
void
reverse_cstring(char* a_string);

/**
 * @brief Returns a copy of a string.
 * @param string String to copy
 * @return A copy of a string
 */
char*
get_strcpy(const char* a_string)
  __attribute__((warn_unused_result));

/**
 * @brief Returns a string reversed.
 * @param string String to reverse
 * @return A string reversed
 */
char*
get_reverse_cstring(const char* a_string)
  __attribute__((warn_unused_result));

/**
 * @brief Trim to the left a string with a character to strip.
 * @param string String to trim to the left
 * @param character_to_strip A character to strip
 */
void
cstring_ltrim(char* a_string,
	      char character_to_strip);

/**
 * @brief Trim to the right a string with a character to strip.
 * @param string String to trim to the right
 * @param character_to_strip A character to strip
 */
void
cstring_rtrim(char* a_string,
	      char character_to_strip);

/**
 * @brief Trim to the left and right a string with a character to strip.
 * @param string String to trim to the left and right
 * @param character_to_strip A character to strip
 */
void
cstring_trim(char* a_string,
	     char character_to_strip);

void
cstring_trim_new_line(char* a_string);

bool
cstring_contains_unsafe(const char* a_string,
			char character);

bool
cstring_contains(const char* a_string,
		 char character);

/**
 * @brief Returns the number of occurrences of a character in a string
 * @param string A string
 * @param character_to_strip A character to count
 * @return The number of occurrences of a character in a string
 */
size_t
get_nb_of_character_of_cstring(const char* a_string,
			       char character);

/**
 * @brief All characters of a string become lower cases.
 * @param string String to lowercase
 */
void
cstring_tolower(char* a_string);

/**
 * @brief All characters of a string become upper cases.
 * @param string String to uppercase
 */
void
cstring_toupper(char* a_string);

/**
 * @brief Returns true if a string contains only digits, otherwise false.
 * @param string A string
 * @return True if a string contains only digits, otherwise false
 */
bool
cstring_contains_only_digits(const char* a_string);

/**
 * @brief Returns true if a trimed string is an integer, otherwise false.
 * @param string A trimed string (no space, \t, etc)
 * @return True if a trimed string is an integer, otherwise false
 */
bool
cstring_is_int_trimed(const char* a_string);

/**
 * @brief Returns true if a string is an integer, otherwise false.
 * @param string A string
 * @return True if a string is an integer, otherwise false
 */
bool
cstring_is_int(const char* a_string);

/**
 * @brief Returns true if a trimed string is an unsigned integer, otherwise false.
 * @param string A trimed string (no space, \t, etc)
 * @return True if a trimed string is an unsigned integer, otherwise false
 */
bool
cstring_is_uint_trimed(const char* a_string);

/**
 * @brief Returns true if a string is an unsigned integer, otherwise false.
 * @param string A string
 * @return True if a string is an unsigned integer, otherwise false
 */
bool
cstring_is_uint(const char* a_string);


EXTERN_C_END


#endif
